<?php
/**
 * Celebrio FileSystem module
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    Celebrio
 * @subpackage FileSystem
 */
namespace Celebrio\FileSystem;

use Nette\Object;

use dibi;

/**
 *
 * @author pavel
 */
class FileAclModel extends Object {

    const ACL_TABLE = 'fpermissions';
    const PRIVILEGES_TABLE = 'fprivileges';
    const RESOURCES_TABLE = 'files';
    const ROLES_TABLE = 'roles';

    public function getRoles() {
        return dibi::fetchAll('SELECT r1.name, r2.name as parent_name
                               FROM ['. self::ROLES_TABLE . '] r1
                               LEFT JOIN ['. self::ROLES_TABLE . '] r2 ON (r1.parent_id = r2.id)
                              ');
    }

    public function getResources() {
        return dibi::fetchAll('SELECT url, name_file FROM ['. self::RESOURCES_TABLE . '] ');
    }

    public function getRules() {
         $query = dibi::query('
            SELECT
                a.allowed as allowed,
                ro.name as role,
                re.name_file as name,
                re.url as url,
                p.name as privilege
                FROM [' . self::ACL_TABLE . '] a
                JOIN [' . self::ROLES_TABLE . '] ro ON (a.id_role = ro.id)
                LEFT JOIN [' . self::RESOURCES_TABLE . '] re ON (a.id_file = re.id_file)
                LEFT JOIN [' . self::PRIVILEGES_TABLE . '] p ON (a.id_fprivilege = p.id_fprivilege)
                ORDER BY a.id_role ASC
        ');
        return $query->fetchAll();
    }
}


